﻿# -*- coding:utf-8 -*-
#
# склад
from django.shortcuts import render_to_response
from django.template import RequestContext, Context
from sklad.models import Category

from sklad.contextprocessors import category_proc




def recursive_get_category_list(category, level):
    res = []
    for cat in category:
        item = {"id":cat.id,"name":cat.name,"parent":cat.parent,"level":level,"csslevel":level*24,}
        category_set = cat.category_set.all()
        item["child_present"] = category_set
        res.append(item)
        subcategory=recursive_get_category_list(category_set, level+1)
        res.extend(subcategory)
    return res



def create_category_tree():
    d = Category.objects.select_related().filter(parent=None)
    category = recursive_get_category_list(d, 0)
    level_prev = 0
    for cat in category:
        level_div = level_prev - cat["level"]
        if level_div < 0:
            cat["level_div"] = range(0) 
        else:
            cat["level_div"] = range(level_div)      
        level_prev = cat["level"]
    return category









def index(request): 
    context = Context({
      'categorys' : create_category_tree(),
    })
    return render_to_response('sklad/index.html', context, context_instance=RequestContext(request, processors=[category_proc]))


def index_alt(request): 
    context = Context({
      #'categorys' : create_category_tree(),
    })
    return render_to_response('sklad/index_alt.html', context, context_instance=RequestContext(request, processors=[category_proc]))

    
    
    